package net.k3rnel.abugadro.gcodes;

import net.k3rnel.abugadro.gcodes.Comment.Comments;
import net.k3rnel.abugadro.gcodes.CommentUpdate.CommentUpdates;
import net.k3rnel.abugadro.providers.ProjectsDB;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentResolver;
import android.content.ContentValues;


public class CommentsFeedHandler extends DefaultHandler{

	private ContentResolver content;
	private ContentValues currentComment;
	private StringBuilder builder;
	private int projectId;
	private int issueId;
	private int commentId;
	private boolean inEntry = false;
	
	// names of the XML tags
	static final String FEED = "feed";
	static final String ENTRY = "entry";
	static final String AUTHOR = "author";
	
	static final String PUB_DATE = "published";
	static final String UP_DATE = "updated";
	static final String ID = "id";
	static final String TITLE = "title";
	static final String CONTENT = "content";
	static final String AUTHOR_NAME = "name";
	static final String AUTHOR_URI = "uri";
	static final String ISSUES_LABEL = "label";
	static final String ISSUES_CC = "ccUpdate";
	static final String ISSUES_STATUS = "status";
	
	public CommentsFeedHandler(ContentResolver content, int projectId, int issueId) {
		this.content = content;
		this.projectId = projectId;
		this.issueId = issueId;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
		if (this.currentComment != null){
			if (localName.equalsIgnoreCase(ID)&&inEntry){
				String id = builder.toString();
				id = id.substring(id.indexOf("comments/full/")+14);
				commentId = Integer.parseInt(id);
				currentComment.put(Comments.COMMENT_ID,id);
			} else if (localName.equalsIgnoreCase(TITLE)&&inEntry){
				currentComment.put(Comments.TITLE,builder.toString());
			} else if (localName.equalsIgnoreCase(CONTENT)&&inEntry){
				currentComment.put(Comments.CONTENT,builder.toString());
			} else if (localName.equalsIgnoreCase(PUB_DATE)&&inEntry){
				currentComment.put(Comments.PUBLISHED,builder.toString());
			} else if (localName.equalsIgnoreCase(UP_DATE)&&inEntry){
				currentComment.put(Comments.UPDATED,builder.toString());
			} else if (localName.equalsIgnoreCase(AUTHOR_NAME)&&inEntry){
				currentComment.put(Comments.AUTHOR,builder.toString());
			}  else if (localName.equalsIgnoreCase(ISSUES_LABEL)&&inEntry){
				String label = builder.toString();
				int changed = 0;
				if(label.startsWith("-")){
					changed = 1;
					label = label.replaceFirst("-","");
				}
				if(!ProjectsDB.getInstance().isCommentUpdateInDB(content, projectId, issueId, commentId, label,changed)){
					ContentValues commUpdate = new ContentValues();
					commUpdate.put(CommentUpdates.ISSUE_ID,issueId);
					commUpdate.put(CommentUpdates.PROJECT_ID,projectId);
					commUpdate.put(CommentUpdates.COMMENT_ID,commentId);
					commUpdate.put(CommentUpdates.CONTENT,label);
					commUpdate.put(CommentUpdates.TYPE,0);
					commUpdate.put(CommentUpdates.POSITIVE,changed);
					content.insert(CommentUpdates.CONTENT_URI, commUpdate);
				}
			} else if (localName.equalsIgnoreCase(ISSUES_STATUS)&&inEntry){
				String label = builder.toString();
				int changed = 0;
				if(label.startsWith("-")){
					changed = 1;
					label = label.replaceFirst("-","");
				}
				if(!ProjectsDB.getInstance().isCommentUpdateInDB(content, projectId, issueId, commentId, label,1)){
					ContentValues commUpdate = new ContentValues();
					commUpdate.put(CommentUpdates.ISSUE_ID,issueId);
					commUpdate.put(CommentUpdates.PROJECT_ID,projectId);
					commUpdate.put(CommentUpdates.COMMENT_ID,commentId);
					commUpdate.put(CommentUpdates.CONTENT,label);
					commUpdate.put(CommentUpdates.TYPE,1);
					commUpdate.put(CommentUpdates.POSITIVE,changed);
					content.insert(CommentUpdates.CONTENT_URI, commUpdate);
				}
			} else if (localName.equalsIgnoreCase(ISSUES_CC)&&inEntry){
				String label = builder.toString();
				int changed = 0;
				if(label.startsWith("-")){
					changed = 1;
					label = label.replaceFirst("-","");
				}
				if(!ProjectsDB.getInstance().isCommentUpdateInDB(content, projectId, issueId, commentId, label,2)){
					ContentValues commUpdate = new ContentValues();
					commUpdate.put(CommentUpdates.ISSUE_ID,issueId);
					commUpdate.put(CommentUpdates.PROJECT_ID,projectId);
					commUpdate.put(CommentUpdates.COMMENT_ID,commentId);
					commUpdate.put(CommentUpdates.CONTENT,label);
					commUpdate.put(CommentUpdates.TYPE,2);
					commUpdate.put(CommentUpdates.POSITIVE,changed);
					content.insert(CommentUpdates.CONTENT_URI, commUpdate);
				}
			}else if (localName.equalsIgnoreCase(ENTRY)&&inEntry){
				inEntry = false;
				if(!ProjectsDB.getInstance().isCommentInDB(content, projectId, issueId, commentId))
					content.insert(Comments.CONTENT_URI,currentComment);
				else
					content.update(Comments.CONTENT_URI,currentComment, Comments.COMMENT_ID +" = "+commentId +" AND " +
							Comments.PROJECT_ID + " = "+projectId+ " AND " + Comments.ISSUE_ID + " = "+issueId, null);	
				currentComment = new ContentValues();
			}
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		if (localName.equalsIgnoreCase(ENTRY)){
			inEntry = true;
			this.currentComment = new ContentValues();
			currentComment.put(Comments.PROJECT_ID,projectId);
			currentComment.put(Comments.ISSUE_ID,issueId);
		}
	}
}

